package com.jd.sunny.java.multithreading.testlockandsynchronize;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Description:
 * @author:sunny
 * @since:2012-12-13 18:05
 * @version:1.0.0
 */
public class Plus {

    private long  number;
    private Lock lock;
    private long max ;

    public Plus(long number,long max) {
        this.number = number;
        this.max = max;
        lock = new ReentrantLock();
    }

    public boolean synchronizePlus(){
        boolean flag =false;
        synchronized (this){
            if(++number >= max){
                if(number == max){
                    System.out.println("END=" + System.currentTimeMillis());
                }
                flag =true;
            }else {
                flag = false;
            }
        }
        return flag;
    }

    /**
     * 返回是否终止线程
     * @return
     */
    public boolean lockPlus(){
        boolean flag =false;
        lock.lock();
        try{
            if(++number >= max){
                if(number == max){
                    System.out.println("END=" + System.currentTimeMillis());
                }
                flag =true;
            }else {
                flag = false;
            }
        }finally {
            lock.unlock();
        }
        return  flag;
    }

}
